package com.freesky.biz.icbc.service.impl;


import com.freesky.biz.icbc.domain.TIcbcNanpingFuli;
import com.freesky.biz.icbc.domain.TIcbcNanpingReferrer;
import com.freesky.biz.icbc.domain.TIcbcNanpingReferrerCoupon;
import com.freesky.biz.icbc.mapper.NanpingPartnerMapper;
import com.freesky.biz.icbc.mapper.NanpingReferrerMapper;
import com.freesky.biz.icbc.service.INanpingPartnerService;
import com.freesky.biz.icbc.service.INanpingReferrerService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.enums.ExportStatusEnum;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.service.ITExportRecordService;
import com.freesky.system.task.SyncReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NanpingPartnerServiceImpl implements INanpingPartnerService {

    @Autowired
    private NanpingPartnerMapper nanpingPartnerMapper;

    @Autowired
    private ITExportRecordService exportRecordService;

    @Autowired
    private SyncReport syncReport;


    @Override
    public List<TIcbcNanpingReferrer> findList(TIcbcNanpingReferrer nanpingReferrer) {
        return nanpingPartnerMapper.findList(nanpingReferrer);
    }

    @Override
    public AjaxResult exportFile(TIcbcNanpingReferrer nanpingReferrer) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("NANPING_PATNER");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(nanpingReferrer, new TIcbcNanpingReferrer(), INanpingReferrerService.class,
                "findList", "推荐人",Long.parseLong(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();
    }

    @Override
    public List<TIcbcNanpingReferrerCoupon> findReferrerCouponList(TIcbcNanpingReferrerCoupon referrerCoupon) {
        return nanpingPartnerMapper.findReferrerCouponList(referrerCoupon);
    }

    @Override
    public AjaxResult exportCouponFile(TIcbcNanpingReferrerCoupon referrerCoupon) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("NANPING_PATNER_COUPON");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(referrerCoupon, new TIcbcNanpingReferrerCoupon(), INanpingReferrerService.class,
                "findReferrerCouponList", "领取记录",Long.parseLong(exportRecord.getId()),sysUser.getUserName());
        return AjaxResult.success();
    }



}
